class: nocopy layout: true <style type="text/css"> .nocopy { webkit-user-select: none; moz-user-select: none; ms-user-select: none; user-select: none; } .remark-slide-content { padding-top: 15px; padding-left: 50px; padding-right: 50px; padding-bottom: 50px; } .remark-slide-content > h1:first-of-type { margin-top: 10px; margin-bottom: 25px; } .remark-slide-content > h2:first-of-type { margin-top: 10px; margin-bottom: 25px; } .remark-slide-content > h3:first-of-type { margin-top: 10px; margin-bottom: 10px; } .reduced_opacity { opacity: 0.1; } .pull-left-45{ float: left; width: 42% } .pull-right-55{ float: right; width: 52% } .bg_aiu { position: relative; z-index: 1; } .bg_aiu::before { content: ""; background-image: url("assets/Snowy_aiu.jpg"); background-size: cover; position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; opacity: 0.4; z-index: -1; } .bg_a1 { position: relative; z-index: 1; } .bg_a1::before { content: ""; background-image: url(https://source.unsplash.com/lEWEQyEN8mU/1600x900); background-size: cover; position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; opacity: 0.4; z-index: -1; } .remark-code, .remark-inline-code {font-size: 60%; } .big { font-size: 1.5em; color: CornFlowerBlue; } .fade1 { opacity: 0.66; } .fade2 { opacity: 0.7; } blockquote { border-left: solid 5px #b31b1b; padding-left: 1em; } .footnote { position: absolute; bottom: 60px; padding-right: 4em; font-size: 0.5em; } </style> --- class: left, bg_aiu <BR/><BR/><BR/><BR/><BR/><BR/> ## ECN339 Finance Data Workshop <hr style="height:4px;border-width:0;color:#B31C1C;background-color:#B31C1C"> <BR/> ### Introduction +<br/>Finance Terms, R, RStudio, Nikkei Needs #### [Clinton Watkins](https://www.clintonwatkins.com)<BR/>Global Business Program, Akita International University #### Winter 2022 --- ## Intro to the course ### Objectives 1. Develop the ability to conduct empirical analyses and interpret results. 2. Gain an understanding of stock market data and selected financial models. 3. Gain familiarity with the methodology of empirical research and coding in R.<p/> ### Useful links - [ECN339 AIMS Page](https://aims.aiu.ac.jp/course/view.php?id=4974) - [ECN339 Slack Workspace invite](https://join.slack.com/t/ecn339w2022/shared_invite/zt-100i0j7fr-SZ6I9NWl2JToVelE_IRPOg) - [Setting up R and RStudio](https://www.clintonwatkins.com/courses/fd/fdw_r_intro_nb_20211218.nb.html). Hopefully you have already done this.<p/> --- ## Syllabus - [Online syllabus](https://www.clintonwatkins.com/courses/fd/syllabus_websitev_20211119.html) on my website and on AIMS.<p/> <iframe src="https://www.clintonwatkins.com/courses/fd/syllabus_websitev_20211119.html" width="100%" height="400px" data-external="1"></iframe> --- ## Topics > We will investigate and model the cross-section of Japanese stock returns with R and RStudio using Nikkei Needs financial data on Tokyo Stock Exchange listed firms.<sup>1<sup/> 1. Introduction - R and RStudio, basics of linear algebra, OLSE estimator. - Finance terms, Nikkei Needs Japanes stocks database. 2. Topic 1: Factor Models - Market model, Fama-French models, principle components. 3. Topic 2: Fama Macbeth model for panel data - Estimating risk premia. 4. Topic 3: Bankruptcy Risk and quantile portfolios - Z-score, O-score 5. Wrap-up and Quiz. .footnote[[1] We will approach the course topics flexibly and they may be adjusted depending on our pace through the course.] --- ## R and RStudio - R is a powerful language suited to statistical analysis, graphics and many other applications. - RStudio provides a convenient interface for using R. - Both are free. We will use R and RStudio for this course. - Hopefully you have already downloaded and set up both using the [guide on my website](https://www.clintonwatkins.com/courses/fd/fdw_r_intro_nb_20211218.nb.html). - [R statistical programming language <img src="data:image/png;base64,#assets/Rlogo.png" height="20px" />](https://www.r-project.org/). - [RStudio <img src="data:image/png;base64,#assets/rstudio-icon.png" height="20px"/>](https://www.rstudio.com/) and [RStudio Cloud](https://rstudio.cloud/). <center><img src="data:image/png;base64,#assets/rstudio1.png" height="250px" /></center> ??? Please download and install this software for the course. --- class: center middle .bg-washed-green.b--dark-green.ba.bw2.br3.shadow-5.ph4.mt5[ Important in this workshop course: Enter and execute the code for the analyses in RStudio as we go through them. If you encounter an error, do not hesitate to ask me for help. ] --- ## If you haven't set up RStudio yet... - Use [RStudio Cloud](https://rstudio.cloud/) for this session. - Make a free account for RStudio Cloud now. - Create a new project for your work in this course. - You will probably need to load some packages. - Do not forget to download any files you create for use later on your own computer. - Set up R and RStudio on your comuter by the next class and do the pre-assignment. --- ## Resources for R .pull-left[ ### Online guides - A comprehensive [introduction to R](https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf). - A textbook [Introduction to Econometrics with R](https://www.econometrics-with-r.org/). - [Documentation and guides for various applications contributed by R users](https://cran.r-project.org/other-docs.html#english). - [Massimiliano's R Page on GitHub](https://github.com/massimilianoj/R_beginners) contains [R for Beginners](https://github.com/massimilianoj/R_beginners/blob/master/R_beginners.pdf), used in my econometrics, finance and macroeconomics classes in Kobe-U. - [Massimiliano](https://link.springer.com/book/10.1007/978-3-030-34529-7) worked for me as a TA and RA in Kobe University. We are working on a book about analysing foreign exchange markets using R. ] .pull-right[ ### CRAN Task Views - The [Comprehensive R Archive Network](https://cran.r-project.org/) (CRAN) Task Views provide information about packages that can be used with R. - Packages extend the functionality of R. They provide routines for various types of data manipulation, analysis, graphics and visualisation, econometric models, and many more putposes. - Packages are constantly being developed and updated by R users. - The most relevant Task View for this course is [Empirical Finance](https://CRAN.R-project.org/view=Finance) ] --- ## Resources for R .pull-left[ ### User community - R is an open source language developed by users and has a huge online community. - A good place to look for Q&A and help is [stackoverflow](https://stackoverflow.com/questions/tagged/r). ### Google is your friend - If you are stuck, [Google search](https://www.google.com/) for information that will help you answer your question. - [R Seek <img src="data:image/png;base64,#assets/Rlogoseek.jpg" height="20px"/>](https://rseek.org/) is a search engine for R. ] .pull-right[ ### R-Bloggers - [<img src="data:image/png;base64,#assets/R_02.jpg" height="20px"/> R-Bloggers](https://www.r-bloggers.com/) contains many useful posts and [Tutorials](https://www.r-bloggers.com/how-to-learn-r-2/) on how to use R for a wide variety of applications (not just finance). ### Books - There are many good books on using R. - Tsay (2014), Ang (2021) and Bennett and Hugen (2016) for finance. - Kleiber and Zeileis (2008) and Pfaff (2008) for econometrics. - Matloff (2011) is good for general programming in R. ] --- ```r library(rfishdraw) file <- system.file("extdata",package = "rfishdraw") dat <- readRDS(paste0(file,"/","output_json.rds")) for (i in seq(length(dat))) {dat[[i]] <- cbind(dat[[i]],i)} plot(NA, xlim = c(0,520),ylim = c(-320,0),axes = F,xlab = " ",ylab = " ") for(i in seq(length(dat))) {lines(x = dat[[i]][,1], y = -dat[[i]][,2], lwd=2, col = "blue")} ``` <img src="data:image/png;base64,#01_Introduction_files/figure-html/unnamed-chunk-3-1.png" width="100%" /> --- ## A map of ... ```r library(leaflet) leaflet() %>% addTiles() %>% setView(140.19838192985657, 39.62662109562309, zoom = 17) ```
--- class: middle ## [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) [(see R-Bloggers)](https://www.r-bloggers.com/2012/11/fast-conways-game-of-life-in-r/) .pull-left-45[ <BR/> <center><img src="data:image/png;base64,#assets/conway1.gif" height="300px" /></center> ] .pull-right-55[ ```r library(caTools) side=500 steps=300 X <- matrix(nrow=side, ncol=side) X[] <- rbinom(side^2,1,0.4) storage <- array(0, c(side, side, steps)) # the simulation for (i in 1:steps) { allW = cbind( rep(0,side), X[,-side]) allNW = rbind(rep(0,side), cbind(rep(0,side-1),X[-side,-side])) allN = rbind(rep(0,side),X[-side,]) allNE = rbind(rep(0,side), cbind(X[-side,-1],rep(0,side-1))) allE = cbind(X[,-1],rep(0,side)) allSE = rbind(cbind(X[-1,-1], rep(0,side-1)),rep(0,side)) allS = rbind(X[-1,],rep(0,side)) allSW = rbind(cbind(rep(0,side-1), X[-1,-side]),rep(0,side)) X2 <- allW + allNW + allN + allNE + allE + allSE + allS + allSW X3 <- X X3[X==0 & X2==3] <- 1 X3[X==1 & X2<2] <- 0 X3[X==1 & X2>3] <- 0 X <- X3 storage[,,i] <- X2 } storage <- storage/max(storage) write.gif(storage, "conway.gif", col="jet", delay=5) ``` ] --- ### Build websites using the blogdown package in R <iframe src="https://www.clintonwatkins.com" width="100%" height="550" data-external="1"></iframe> --- ### Shiny apps built using R <iframe src="https://clinton.shinyapps.io/ExpectedUtility/" width="100%" height="550" data-external="1"></iframe> --- ### Shiny app examples... <iframe src="https://shiny.rstudio.com/gallery/" width="100%" height="550" data-external="1"></iframe> --- ### Top 8 things to know about R 1. R is an *object oriented language*. Everything in R is an object with particular properties that can interact with other objects in certain ways. Understanding the different types of objects is key to using them. E.g. vector, matrix, data frame, list, function. 2. The *assignment operator* is used to assign values to an object. ```r a <- 2 b <- 3 a+b ``` ``` ## [1] 5 ``` 3. *Packages* extend the functionality of base R. Packages contain code (functions), data and documents. To use a package it must be installed (once) and then loaded each session when you want to use it. Find information about packages from Task Views or internet search. ```r install.packages("leaflet") library(leaflet) ``` 4. When you have an *error* executing your code, 99% of the time it will be because (i) you mistyped the code or (ii) you have not loaded a package required for a function to work. --- ### Top 8 things to know about R <ol start=5> <li> The *working directory* is where R will look for and save files. ```r getwd() ``` ``` ## [1] "/Users/clinton/Dropbox/syncd_r_data/FDW/lectures/01-introduction/lecture" ``` ```r setwd("/Users/clinton/Dropbox/syncd_r_data/FDW/preparation") ``` </li> <li> Lots of documentation and help is available within R. Try: ```r ?seq help(seq) example(seq) help(package - AER) help (topic = "Arithmetic") help.search("regression") ??regression ``` </li><p/> <li> *Reproducible research:* do your work in an R notebook, including explanation, code and output. This way your work can be explained to tohers and will be reproducible later, even when you may have forgotten exactly what you did. </li><BR/> <li> Practice *writing code* as much as possible. It will be frustrating at first, but your skills will improve rapidly if you keep at it. </li> <ol> --- class: center middle .bg-washed-green.b--dark-green.ba.bw2.br3.shadow-5.ph4.mt5[ The only way to write good code is to write tons of bad code first. Feeling shame about bad code stops you from getting to good code. .tr[ — Hadley Wickham ]] --- ### R Notebooks .pull-left[ - I've uploaded an R Notebook template to AIMS showing many of the useful features. - Download the template and use during the class and for your homework. - Open a new Notebook from the menu at the top-right of the RStudio toolbar or write in the template I provided. - A Notebook consists of the YAML header, text, code chunks and output. - Cut, paste and modify examples from the template. - Saving the .Rmd file produces or updates the .html file Notebook. - Remember to run all code (menu: Run All) before saving your Notebook. ] .pull-right[ <center><img src="data:image/png;base64,#assets/rmd-eg.png" height="270px" /></center> *** <center><img src="data:image/png;base64,#assets/nb-eg.png" height="270px" /></center> ] --- ### Basics of linear algebra in R - Knowing some basics of linear algebra will help you understand both how your R code works and the theory behind the financial econometric models that we will cover. - A scalar object that represents a single value (numeric, character, logical). ```r a <- sample(1:10, 1) a ``` ``` ## [1] 7 ``` - A vector is a sequence of elements of the same data type (numeric, character, logical). `$$X = \begin{bmatrix} x_1\\ x_2\\ \vdots\\ x_n \end{bmatrix}$$` - Create a vector by concatenation: ```r x <- c(1.8, 3.14, 4, 88.169, 13) x ``` ``` ## [1] 1.800 3.140 4.000 88.169 13.000 ``` --- ### Vectors - What is the length of the vector? ```r length(x) ``` ``` ## [1] 5 ``` - Subsetting the first and fourth elements using square brackets. ```r x[c(1,4)] ``` ``` ## [1] 1.800 88.169 ``` - Or subset this way: ```r x[-c(2,3,5)] ``` ``` ## [1] 1.800 88.169 ``` --- ### Vector Arithmetic - Arithmetic with scalars. ```r z <- 2*x+3 z ``` ``` ## [1] 6.600 9.280 11.000 179.338 29.000 ``` - Arithmetic with vectors in R. ```r z+x ``` ``` ## [1] 8.400 12.420 15.000 267.507 42.000 ``` ```r x2 <- c(1,2,3) x3 <- c(1,2,3,4,5) x2+x3 ``` ``` ## [1] 2 4 6 5 7 ``` - We can use various operators on vectors, e.g.: ```r log(x) ``` ``` ## [1] 0.5877867 1.1442228 1.3862944 4.4792554 2.5649494 ``` --- ### Sequences in R - Generate a sequence: ```r 5:1 ``` ``` ## [1] 5 4 3 2 1 ``` - Using the seq function: ```r seq(1,5,1) ``` ``` ## [1] 1 2 3 4 5 ``` - Include the sequence in vector arithmetic: ```r 5:1*x+1:5 ``` ``` ## [1] 10.000 14.560 15.000 180.338 18.000 ``` --- ### Patterned vectors in R - Various functions can be used to create patterned vectors. ```r ones <- rep(1,10) ones ``` ``` ## [1] 1 1 1 1 1 1 1 1 1 1 ``` ```r even <- seq(from=2, to=20, by=2) even ``` ``` ## [1] 2 4 6 8 10 12 14 16 18 20 ``` ```r trend <- 1981:2005 trend ``` ``` ## [1] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 ## [16] 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 ``` - Combine two vectors: ```r c(ones,even) ``` ``` ## [1] 1 1 1 1 1 1 1 1 1 1 2 4 6 8 10 12 14 16 18 20 ``` --- ### Matrices .pull-left[ - A matrix is a two dimensional array of elements. <BR/> `$$A = \begin{bmatrix} 1, 3, 5\\ 2, 4, 6 \end{bmatrix}$$` <BR/> `$$B = \begin{bmatrix} 2, 6\\ 4, 8 \end{bmatrix}$$` <BR/> - The dimension of a matrix are found by counting the number of rows and columns. - A is a 2 by 3 matrix. - B is a square 2 by 2 matrix. ] .pull-right[ - Creating a matrix in R: ```r A <- matrix(1:6, nrow=2, byrow = FALSE) A ``` ``` ## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6 ``` ```r B <- matrix(c(2,4,6,8), ncol=2, byrow = FALSE) B ``` ``` ## [,1] [,2] ## [1,] 2 6 ## [2,] 4 8 ``` - Dimensions: ```r dim(A) ``` ``` ## [1] 2 3 ``` ```r dim(B) ``` ``` ## [1] 2 2 ``` ] --- ### Matrix operators Transpose a matrix: ```r A ``` ``` ## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6 ``` ```r t(A) ``` ``` ## [,1] [,2] ## [1,] 1 2 ## [2,] 3 4 ## [3,] 5 6 ``` --- ### Dimensions Extract the dimensions of a matrix: ```r dim(A) ``` ``` ## [1] 2 3 ``` ```r dim(t(A)) ``` ``` ## [1] 3 2 ``` ```r nrow(A) ``` ``` ## [1] 2 ``` ```r ncol(A) ``` ``` ## [1] 3 ``` --- ## Subsetting a Matrix We can subset a matrix the same way that we subset a vector. ```r A ``` ``` ## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6 ``` ```r A[2,3] ``` ``` ## [1] 6 ``` --- ## Subsetting a Matrix For rows 1 and 2, and columns 1 and 3: ```r A1 <- A[1:2,c(1,3)] A1 ``` ``` ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 6 ``` ```r A1 <- A[1:2,c(1,3)] A1 ``` ``` ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 6 ``` \vspace{18pt} Or removing column 2 does the same thing: ```r A[,-2] ``` ``` ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 6 ``` --- ### Determinant --- ### Inverse of a Matrix ```r A1 ``` ``` ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 6 ``` ```r solve(A1) ``` ``` ## [,1] [,2] ## [1,] -1.5 1.25 ## [2,] 0.5 -0.25 ``` \vspace{18pt} A matrix multiplied by its inverse gives the identity matrix. ```r A1 %*% solve(A1) ``` ``` ## [,1] [,2] ## [1,] 1 0 ## [2,] 0 1 ``` --- ### Matrix Arithmetic Operators ```r A1 ``` ``` ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 6 ``` ```r B ``` ``` ## [,1] [,2] ## [1,] 2 6 ## [2,] 4 8 ``` ```r A1+B ``` ``` ## [,1] [,2] ## [1,] 3 11 ## [2,] 6 14 ``` --- ### Matrix Arithmetic Operators Element by element multiplication: ```r A1*B ``` ``` ## [,1] [,2] ## [1,] 2 30 ## [2,] 8 48 ``` \vspace{18pt} Matrix multiplication: ```r A1%*%B ``` ``` ## [,1] [,2] ## [1,] 22 46 ## [2,] 28 60 ``` --- ## Matrix Arithmetic Operators ```r A ``` ``` ## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6 ``` ```r B ``` ``` ## [,1] [,2] ## [1,] 2 6 ## [2,] 4 8 ``` ```r A%*%B ``` ```r t(A)%*%B ``` ``` ## [,1] [,2] ## [1,] 10 22 ## [2,] 22 50 ## [3,] 34 78 ``` --- ### Patterned Matrices Create a diagonal matrix: ```r diag(4) ``` ``` ## [,1] [,2] [,3] [,4] ## [1,] 1 0 0 0 ## [2,] 0 1 0 0 ## [3,] 0 0 1 0 ## [4,] 0 0 0 1 ``` ```r diag(1,4,4) ``` ``` ## [,1] [,2] [,3] [,4] ## [1,] 1 0 0 0 ## [2,] 0 1 0 0 ## [3,] 0 0 1 0 ## [4,] 0 0 0 1 ``` --- ### Patterned Matrices ```r diag(rep(c(1,2),c(6,6))) ``` ``` ## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] ## [1,] 1 0 0 0 0 0 0 0 0 0 0 0 ## [2,] 0 1 0 0 0 0 0 0 0 0 0 0 ## [3,] 0 0 1 0 0 0 0 0 0 0 0 0 ## [4,] 0 0 0 1 0 0 0 0 0 0 0 0 ## [5,] 0 0 0 0 1 0 0 0 0 0 0 0 ## [6,] 0 0 0 0 0 1 0 0 0 0 0 0 ## [7,] 0 0 0 0 0 0 2 0 0 0 0 0 ## [8,] 0 0 0 0 0 0 0 2 0 0 0 0 ## [9,] 0 0 0 0 0 0 0 0 2 0 0 0 ## [10,] 0 0 0 0 0 0 0 0 0 2 0 0 ## [11,] 0 0 0 0 0 0 0 0 0 0 2 0 ## [12,] 0 0 0 0 0 0 0 0 0 0 0 2 ``` --- ### Patterned matrices Extract the diagonal elements of a matrix: ```r A1 ``` ``` ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 6 ``` ```r diag(A1) ``` ``` ## [1] 1 6 ``` --- --- ## Linear regression model - The linear regression model in matrix form. Consider as the unknown "true" model that we want to know about. $$ y = X\beta + \epsilon $$ - Where `\(y\)` is the dependent variable, `\(X\)` contains explanatory variables, `\(\beta\)` contains the parameters to be estimated and `\(\epsilon\)` is a stochastic disturbance or random error term. <BR/> `$$\underset{n\times 1}y = \begin{bmatrix} Y_1\\ Y_2\\ \vdots\\ Y_n \end{bmatrix} ,\quad \underset{n\times (k+1)}X = \begin{bmatrix} 1, X_{11}, X_{12}, \dots, X_{k1}\\ 1, X_{21}, X_{22}, \dots, X_{k2}\\ \vdots\\ 1, X_{n1}, X_{n2}, \dots, X_{kn} \end{bmatrix} ,\quad \underset{(k+1)\times 1}\beta = \begin{bmatrix} \beta_1\\ \beta_2\\ \vdots\\ \beta_k \end{bmatrix} ,\quad \underset{n\times 1}\epsilon = \begin{bmatrix} \epsilon_1\\ \epsilon_2\\ \vdots\\ \epsilon_n \end{bmatrix}$$` <BR/> - We have `\(n\)` observations of the dependent variable, `\(n\)` observations of `\(k\)` explanatory variables, an intercept term (the column of ones in `\(X\)`), and `\(k+1\)` parameters (the intercept plus the coefficients of our `\(k\)` explanatory variables). --- ## Linear regression model - example - Assume a model with an intercept, two explanatory variables and we have 30 observations. `$$y_i = \beta_1 + \beta_2 X_{i1} + \beta_3 X_{i2} + \epsilon_i \quad i=1,\dots,30$$` - Written in matrix form: $$ y = X\beta + \epsilon $$ <BR/> `$$\underset{30\times 1}y = \begin{bmatrix} Y_1\\ Y_2\\ \vdots\\ Y_{30} \end{bmatrix} ,\quad \underset{30\times 3}X = \begin{bmatrix} 1, X_{11}, X_{12}\\ 1, X_{21}, X_{22}\\ \vdots\\ 1, X_{30,1}, X_{30,2} \end{bmatrix} ,\quad \underset{3\times 1}\beta = \begin{bmatrix} \beta_1\\ \beta_2\\ \beta_3 \end{bmatrix} ,\quad \underset{30\times 1}\epsilon = \begin{bmatrix} \epsilon_1\\ \epsilon_2\\ \vdots\\ \epsilon_{30} \end{bmatrix}$$` --- ## Ordinary Least Squares Estimation - Our aim is to generate the best possible estimates of the parameters, `\(\beta\)`. - Our estimate of `\(\beta\)` is denoted `\(\hat\beta\)` and the estimate of the error term is the residual `\(e\)`. The residual contains what our model does not explain about `\(y\)`. `$$e=y-X\hat\beta$$` - Ordinary Least Squares (OLS) estimation simply says select the `\(\hat\beta\)` that minimises what our model (in particular, `\(X\hat\beta\)`) does not explain about our dependent variable `\(y\)`. - We do this by finding the `\(\hat\beta\)` that minimises the sum of the squared residuals, `\(e^\prime e\)`. - Note that the *fitted values* of our regression are `\(\hat y = X\hat\beta\)`. - Next we derive the Ordinary Least Squares Estimator (OLSE). --- ## Ordinary Least Squares Estimation - Sum of the squared residuals, `\(e^{\prime}e\)`: `$$\begin{align}\underset{1\times n}{e^{\prime}} \times \underset{n\times 1}{e} &= \begin{bmatrix} e_1\: e_2\: \dots\: e_n \end{bmatrix} \begin{bmatrix} e_1\\ e_2\\ \dots\\ e_n \end{bmatrix}\\[9pt] &=\underset{1\times 1}{\begin{bmatrix} e_1\times e_1\, +\, e_2\times e_2\, +\, \dots,\, +\, e_n\times e_n \end{bmatrix}}\end{align}$$` - Write the sum of the squared residuals in terms of our data and the parameter estimates. - Note that the transpose of a scalar is a scalar, so `\(\hat{\beta^{\prime}}X^{\prime}y=y^{\prime}X\hat\beta\)`. `$$\begin{align}e^{\prime}e &= {(y-X\hat\beta)}^{\prime}(y-X\hat\beta)\\ &= y^{\prime}y - \hat{\beta^{\prime}}X^{\prime}y - y^{\prime}X\hat\beta +\hat{\beta^{\prime}}X^{\prime}X\hat\beta\\ &= y^{\prime}y - 2\hat{\beta^{\prime}}X^{\prime}y + \hat{\beta^{\prime}}X^{\prime}X\hat\beta\end{align}$$` --- ## Ordinary Least Squares Estimation - To find the `\(\hat\beta\)` that minimises the sum of the squared residuals, differentiate `\(e'e\)` with respect to `\(\hat\beta\)` and set equal to zero. `$$\begin{align}\frac{\partial e^{\prime}e}{\partial \hat\beta} &= -2X^{\prime}y + 2X^{\prime}X\hat\beta = 0 \end{align}$$` `$$X^{\prime}X\hat\beta = X^{\prime}y$$` - Pre-multiply each side by `\({(X^{\prime}X)}^{-1}\)`: `$${(X^{\prime}X)}^{-1}X^{\prime}X\hat\beta={(X^{\prime}X)}^{-1}X^{\prime}y$$` - The OLSE of `\(\beta\)` is `\(\hat\beta\)`: `$$\hat\beta={(X^{\prime}X)}^{-1}X^{\prime}y$$` --- ## Finance terms - We will analyse stock markets in this course. - A stock represents<sup>2</sup> - [Investopedia](https://www.investopedia.com/) is a good place to look up finance terms. .footnote[[2] Text associated with footnote 1.] --- --- ## References Ang, C. S. (2021). _Analyzing Financial Data and Implementing Financial Models Using R_. Second. Springer Texts in Business and Economics. Cham: Springer International Publishing. ISBN: 978-3-030-64154-2. DOI: [10.1007/978-3-030-64155-9](https://doi.org/10.1007%2F978-3-030-64155-9). URL: [https://link.springer.com/10.1007/978-3-030-64155-9](https://link.springer.com/10.1007/978-3-030-64155-9). Bennett, M. J. and D. L. Hugen (2016). _Financial Analytics with R: Building a Laptop Laboratory for Data Science_. Cambridge University Press. ISBN: 9781107150751. DOI: [https://doi.org/10.1017/CBO9781316584460](https://doi.org/https%3A%2F%2Fdoi.org%2F10.1017%2FCBO9781316584460). Kleiber, C. and A. Zeileis (2008). _Applied Econometrics with R_. 1st. New York: Springer US. ISBN: 978-0-387-77316-2. DOI: [10.1007/978-0-387-77318-6](https://doi.org/10.1007%2F978-0-387-77318-6). eprint: arXiv:1011.1669v3. URL: [http://link.springer.com/10.1007/978-0-387-77318-6 http://www.springer.com/gp/book/9780387773162](http://link.springer.com/10.1007/978-0-387-77318-6 http://www.springer.com/gp/book/9780387773162). Matloff, N. (2011). _The Art of R Programming: A Tour of Statistical Software Design_. No Starch Press. ISBN: 9781593273842. URL: [https://www.nostarch.com/artofr.htm](https://www.nostarch.com/artofr.htm). Pfaff, B. (2008). _Analysis of Integrated and Cointegrated Time Series with R_. Second. New York: Springer-Verlag. ISBN: 978-0-387-75966-1. URL: [papers2://publication/uuid/2D6BDE05-D712-4D3C-99CD-72EADA215C83 http://www.springer.com/gp/book/9780387759661](papers2://publication/uuid/2D6BDE05-D712-4D3C-99CD-72EADA215C83 http://www.springer.com/gp/book/9780387759661). --- Tsay, R. S. (2014). _Multivariate time series analysis : with R and financial applications_. Wiley. ISBN: 9781118617908. --- ## Appendix - Locale issues: ```r Sys.setlocale("LC_ALL", "en_US.UTF-8") ``` --- ### Extra screen rec. - [Amazon](https://www.amazon.co.jp/dp/B07ZNTHZHZ) <center><img src="data:image/png;base64,#assets/screen.png" height="500px" /></center>